/**
 * 
 */
package cn.ad.server.web.spring.service.actions;

import cn.ad.server.persistence.GuidRepository;
import cn.ad.server.web.spring.model.*;
import cn.ad.server.web.spring.model.dactive.DactiveAdResponse;
import cn.ad.server.web.spring.service.transmitter.AbstractBaseTransmitter;
import cn.ad.server.biz.internal.AdService;
import cn.ad.server.domain.ad.GuidValueObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

/**
 * 激活回调-转发处理服务。
 * @author wangwei
 */
public abstract class DactiveRequestAction implements IAdRequestAction{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(DactiveRequestAction.class);
	
	@Autowired
	private AdService adService;
	
//	@Autowired
//	private AdSpaceRepository adSpaceRepository;
	
	@Autowired
	private GuidRepository guidRepository;
	
	/**
	 * 处理入口
	 */
	@Override
	public AdResponse doRequest(AdRequest request) throws RequestActionException{
		DactiveAdResponse resp= new DactiveAdResponse(0) {};
		try{
			if(request instanceof AppflyerRequest){
				AppflyerRequest req = (AppflyerRequest)request;
				return process(req,resp);
			}else if(request instanceof TalkingDataRequest){
				TalkingDataRequest req = (TalkingDataRequest)request;
				return process(req,resp);
			}else if(request instanceof AvidRequest){
				AvidRequest req = (AvidRequest)request;
				return process(req,resp);
			}else{
				throw new ClassCastException();
			}
		}finally{
			adService.unbond();
		}
	}

	/**
	 * 处理
	 * @param adRequest
	 * @throws RequestActionException 
	 */
	private AdResponse process(AdRequest adRequest,AdResponse resp) throws RequestActionException {
			doProcess(adRequest, resp);
			AbstractBaseTransmitter transmitter = getTransmitter(adRequest,resp);
			if (transmitter==null) {//不是通过补量渠道发来的信息。
				return resp;
			}
			BaseTransmitInfo transmitInfo = convertTransmitInfo(adRequest, resp);
			try {
				AdResponse resp2 = sendService(transmitter,transmitInfo);
			} catch (Exception e) {
				throw new RequestActionException(e.getCause());
			}
			return resp;
	}
	
	/**
	 * 获取发送的信息。
	 * 
	 * @param request
	 * @param channelId
	 * @return
	 */
	public abstract BaseTransmitInfo convertTransmitInfo(AdRequest request,AdResponse resp);
	
	public void doProcess(AdRequest adRequest,AdResponse resp){
		
	}


	/**
	 * 获取第三方补量transmitter
	 * @param adRequest
	 * @return
	 */
	public abstract AbstractBaseTransmitter getTransmitter(AdRequest adRequest,AdResponse resp);
	
	
	/**
	 * 发送服务。
	 * @param transmitter
	 * @param info
	 * @return
	 */
	AdResponse sendService(AbstractBaseTransmitter transmitter,ITransmitInfo info) {
		AdResponse resp=null;
		try {
			resp = transmitter.transmit(info);
		}finally{
			if(resp==null){
				resp = new AdResponse(204){};//未知错误。
			}
			//TODO 日志记录。
			LOGGER.info(" ch: "+transmitter.getCh()+" url: "+transmitter.getUrl()+" parameter: "+info.getTransmitInfo()+" response: "+resp.getStatus());
		}
		return resp;
	}
	
	/**
	 * 根据guid获取GuidValueObject
	 * 
	 * @param guid
	 * @return
	 */
	protected GuidValueObject getGuidData(String guid) {
		if (!StringUtils.isEmpty(guid)) {
			return guidRepository.findGuidVo(guid);
		}
		return null;
	}

	
//	/**
//	 * 获取第三方补量地址
//	 * @param adRequest
//	 * @return
//	 */
//	private String getThirdPartyAddress(AdCallBackRequest adRequest) {
//		LOGGER.debug("AdTrackServlet doService 第三方渠道数据");
//		String channelId = adRequest.getChannelID();
//		String  thirdPartyAddress= channelUrl.get("ch/"+channelId);
//		LOGGER.debug("第三方渠道发送的地址参数："+thirdPartyAddress+"?" + adRequest.getTransmitInfo());
//		return thirdPartyAddress;
//	}
//	
	
//	
//	/**
//	 * 获取设备信息。
//	 * @param trackCommand
//	 * @return
//	 */
//	private String getDeviceInfo(AdCallBackRequest trackCommand){
//		return trackCommand.toUrlDeviceInfo();
//	}	
//	
//	/**
//	 *第三方地址 
//	 */
//	@Resource(name="channelUrl")
//	private Map<String,String> channelUrl;
	
//	/**
//	 *连接管理器 
//	 */
//	@Autowired
//	private HttpConnectionManager httpConnectionManager;


}
